package com.dayi.activity.chatroom.childfragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dayi.R;
import com.dayi.R2;
import com.dayi.bean.AnswerDetail;
import com.lib.app.ARouterPathUtils;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

@Route(path = ARouterPathUtils.Dayi_FragmentStudentTeacherSecond)
public class TeacherSecondFragment extends BaseAppFragment {

    @BindView(R2.id.iv_question)
    ImageView ivQuestion;
    @BindView(R2.id.rv_message)
    RecyclerView rvMessage;

    ChatMessageAdapter messageAdapter;

    private List<AnswerDetail> messageList = new ArrayList<>();

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        initStudentChat();

    }

    public void updateSecondChatMsg(AnswerDetail answerDetail) {
        if (answerDetail != null) {
            messageList.add(answerDetail);
        }
        messageAdapter.notifyDataSetChanged();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_student;
    }

    private void initStudentChat() {

        AnswerDetail answerDetail1 = new AnswerDetail();
        AnswerDetail answerDetail2 = new AnswerDetail();

        AnswerDetail.ObjBean objBean1 = new AnswerDetail.ObjBean();
        objBean1.setIs_check(2);
        objBean1.setCorrect("老师发送的");
        answerDetail1.setObj(objBean1);

        AnswerDetail.ObjBean objBean2 = new AnswerDetail.ObjBean();
        objBean2.setIs_check(1);
        objBean2.setCorrect("学生发送的");
        answerDetail2.setObj(objBean2);

        messageList.add(answerDetail1);
        messageList.add(answerDetail2);

        messageAdapter = new ChatMessageAdapter(getActivity(), messageList);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(getActivity());
        rvMessage.setLayoutManager(manager);
        rvMessage.setAdapter(messageAdapter);

    }


    class ChatMessageAdapter extends BaseAdapter<AnswerDetail> {

        public ChatMessageAdapter(Context context, List<AnswerDetail> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_chat_message;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<AnswerDetail> mData) {

            LinearLayout linReceive = holder.getView(R.id.lin_receive);
            TextView tvReceive = holder.getView(R.id.tv_receive);
            ImageView ivReceive = holder.getView(R.id.iv_receive);
            //收到消息，根据类型向该容器中添加相应控件，文字--图片--语音
            LinearLayout linReveiveMsg = holder.getView(R.id.lin_receive_msg);


            LinearLayout linSend = holder.getView(R.id.lin_send);
            TextView tvSend = holder.getView(R.id.tv_send);
            ImageView ivSend = holder.getView(R.id.iv_send);
            //发送消息，根据类型向该容器中添加相应控件，文字--图片--语音
            LinearLayout linSendMsg = holder.getView(R.id.lin_send_msg);

            //假设1代表发送
            if (mData.get(position).getObj().getIs_check() == 1) {
                linReceive.setVisibility(View.GONE);

                tvSend.setText(mData.get(position).getObj().getCorrect());

            } else {
                linSend.setVisibility(View.GONE);
                tvReceive.setText(mData.get(position).getObj().getCorrect());
            }

        }
    }


}
